package server;

import java.util.ArrayList;

public class TicTacToeRoom implements IGameRoom
{

    String gameStatus;      //can be: "on" "finished" "standoff"
    String activePlayer;    //contains the name of the player, that does the next step
    String winnerName;      //contains the name of the player, that won
    int[] matrix;           //0 - O, 1 - X, 2 - empty
    private ArrayList<Player> players = new ArrayList<>();

    /** Class constructor.
     *
     * @param p1 the first player X
     * @param p2 the second player O
     */
    public TicTacToeRoom(Player p1, Player p2)
    {
        matrix = new int[9];
        for (int i = 0; i < 9; i++)
        {
            matrix[i] = 2; //every cell is "empty" at the beginning
        }
        players.add(p1);
        players.add(p2);
        activePlayer = p1.name;
        this.gameStatus = "on";
    }

    public void doMove(String playerName, int n)
    {
        if (playerName.equals(activePlayer)) //если правильный игрок хочет сделать ход
        {
            if (matrix[n] == 2) //если желаемая ячейка пуста
            {
                if (activePlayer.equals(players.get(0).name)) //для первого игрока
                {
                    matrix[n] = 1; //ставим крестик
                } else
                {
                    matrix[n] = 0; //для второго игрока - ставим нолик
                }
                updateGameStatus();
            }
        }
    }

    private void updateGameStatus()
    {
        if (checkWinForPlayer(1) == true)
        {
            this.gameStatus = "finished";
            this.winnerName = players.get(0).name; //the first player won
        } 
        else if (checkWinForPlayer(0) == true)
        {
            this.gameStatus = "finished";
            this.winnerName = players.get(1).name; //the second player won
        } 
        else
        {
            //проверка на ничью
            boolean isStandoff = true;
            for (int i : matrix)
            {
                if (i == 2) //если есть хоть одна пустая ячейка
                {
                    isStandoff = false; //то нет пока ничьей, игра продолжается
                }
            }
            if (isStandoff == true)
            {
                this.gameStatus = "standoff";
            }
        }
    }

    private boolean checkWinForPlayer(int k)
    {
        boolean ret = false;
        if ((matrix[0] == k && matrix[1] == k && matrix[2] == k)
                || (matrix[3] == k && matrix[4] == k && matrix[5] == k)
                || (matrix[6] == k && matrix[7] == k && matrix[8] == k)
                || (matrix[0] == k && matrix[3] == k && matrix[6] == k)
                || (matrix[1] == k && matrix[4] == k && matrix[7] == k)
                || (matrix[2] == k && matrix[5] == k && matrix[8] == k)
                || (matrix[0] == k && matrix[4] == k && matrix[8] == k)
                || (matrix[2] == k && matrix[4] == k && matrix[6] == k))
        {
            ret = true;
        }
        return ret;
    }

    public String getMatrix()
    {
        //int;int;int;int;int;int;int;int;int;
        String ret="";
        for(int k:matrix)
        {
            ret+=""+k;
            ret+=";";
        }        
        return ret;
    }
}
